package ru.spb.osll.myfit.server.handler;

import org.apache.log4j.Logger;
import ru.spb.osll.myfit.common.Include;
import ru.spb.osll.myfit.common.Page;
import ru.spb.osll.myfit.common.PageElement;
import ru.spb.osll.myfit.common.PageElementType;
import ru.spb.osll.myfit.server.parser.PageUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Vassar Date: 23.05.2011 Time: 19:49:41 To
 * change this template use File | Settings | File Templates.
 */
public class ParentVariablesCreator {
  private static Logger LOG = Logger.getLogger(PageLoader.class);
  // private String m_root;
  private String CONTENT = "content.txt";
  private PageLoader pageLoader;

  // private PageParser m_parser;

  public ParentVariablesCreator(PageLoader loader) {
    // m_root = Configurator.getInstance().getRoot();
    pageLoader = loader;
    // m_root = root;
    // m_parser = new PageParser(new Converter());
  }

  public HashMap<String, String> getParentVariablesMap(String fileName,
                                                       String m_root) {
    HashMap<String, String> parentVariables = new HashMap<String, String>();
    List<String> parentList = new ArrayList<String>();
    String path = fileName.substring(0,
        fileName.indexOf(m_root) + m_root.length());

    if (!fileName.equals(path + File.separator + CONTENT)) {
      parentList.add(path + File.separator + CONTENT);
      getListPath(parentList, path, fileName);
      parentVariables = getParentVariables(parentList, m_root);
    }

    return parentVariables;
  }

  protected void getListPath(List<String> list, String path, String fileName) {
    String str = fileName.substring(path.length() + 1);
    str = str.substring(0, str.indexOf(File.separatorChar));
    if (!fileName
        .equals(path + File.separator + str + File.separator + CONTENT)) {
      list.add(path + File.separator + str + File.separator + CONTENT);
      getListPath(list, path + File.separator + str, fileName);
    }
  }

  protected HashMap<String, String> getParentVariables(List<String> list,
                                                     String m_root) {
    HashMap<String, String> map = new HashMap<String, String>();
    for (String path : list) {
      try {
        Page page = loadPageWithoutParentVariables(path, m_root);
        if (page.hasElements())
          map.putAll(page.getVariables());
      } catch (IOException e) {
        LOG.error("File \"" + path + "\" does not exist!");
      }
    }
    return map;
  }

  public Page loadPageWithoutParentVariables(String fileName, String m_root)
      throws IOException {
    Page page = pageLoader.loadPage(fileName);
    for (PageElement element : page.getElements()) {
      if (isInclude(element)) {
        String includePath = PageUtils.getFullPath(m_root, ((Include) element).getPath());
        Page includePage;
        try{
          includePage = loadPageWithoutParentVariables(includePath, m_root);
          if (includePage.hasElements()) {
            page.addVariables(includePage.getVariables());
          }
        } catch (IOException e){
          LOG.error("File \"" + includePath + "\" does not exist!");
        }
      }
    }

    return page;
  }

  private boolean isInclude(PageElement element){
      PageElementType type = element.getType();
      return (type==PageElementType.INCLUDE ||
              type==PageElementType.SET_UP ||
              type==PageElementType.TEAR_DOWN);
  }
}
